//
//  HMMainViewController.swift
//  WeiBo
//
//  Created by Apple on 2017/5/30.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //  设置自定义tabbar
        let hmtabBar = HMTabBar()
        //  设置代理
        hmtabBar.hmDelegate = self
        
        hmtabBar.composeButtonCallBack = { [unowned self] in
            print("我是闭包回调的")
            print(self)
        }
        
        //  使用kvc给只读属性赋值
        self.setValue(hmtabBar, forKey: "tabBar")
        addChildViewController(childController: HMHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(childController: HMMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(childController: HMDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(childController: HMMeTableViewController(), imageName: "tabbar_profile", title: "我的")
    }
    
    //  添加子控制器
    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        
        //  设置tabbaritem的文字和图片
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //  设置文字
//        childController.tabBarItem.title = title
//        //  设置导航栏文字
//        childController.navigationItem.title = title

        //  合并成一句代码
        childController.title = title
        //  设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        //  设置选中文字的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        
        //  创建导航控制器
        let nav = HMNavigationController(rootViewController: childController)
        
        //  添加导航控制器
        addChildViewController(nav)
        
        
    }

    

}

extension HMMainViewController: HMTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
}

